using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace SharpCore.NullableTypes
{
	/// <summary>
	/// Wraps the <see cref="System.Decimal"/> value type in an object so that references can be null.
	/// </summary>
	[Serializable]
	public sealed class NullableDecimal : INullable
	{
		/// <summary>
		/// The <see cref="System.Decimal"/> value this instance represents.
		/// </summary>
		private decimal decimalValue;

		/// <summary>
		/// Initializes a new instance of the NullableDecimal class.
		/// </summary>
		public NullableDecimal()
		{
			this.decimalValue = 0;
		}

		/// <summary>
		/// Initializes a new instance of the NullableDecimal class with a <see cref="System.Decimal"/> value.
		/// </summary>
		/// <param name="decimalValue">The <see cref="System.Decimal"/> that this instance represents.</param>
		public NullableDecimal(decimal decimalValue)
		{
			this.decimalValue = decimalValue;
		}

		/// <summary>
		/// Initializes a new instance of the NullableDecimal class with a <see cref="System.String"/> that represents a <see cref="System.Decimal"/>.
		/// </summary>
		/// <param name="decimalValue">The string representation of a <see cref="System.Decimal"/> value.</param>
		public NullableDecimal(string decimalValue)
		{
			this.decimalValue = Decimal.Parse(decimalValue);
		}

		/// <summary>
		/// Creates a NullableDecimal that is serializable with the specified SerializationInfo and StreamingContext.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableDecimal.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableDecimal.</param>
		private NullableDecimal(SerializationInfo info, StreamingContext context)
		{
			this.decimalValue = (decimal) info.GetDecimal("Value");
		}

		/// <summary>
		/// Returns the data needed to serialize the NullableDecimal.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableDecimal.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableDecimal.</param>
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Value", decimalValue);
		}

		/// <summary>
		/// Gets or Sets the <see cref="System.Decimal"/> value this instance represents.
		/// </summary>
		public decimal DecimalValue
		{
			get { return decimalValue; }
			set { decimalValue = value; }
		}

		/// <summary>
		/// Converts a <see cref="System.Decimal"/> to a NullableDecimal.
		/// </summary>
		/// <param name="decimalValue">The <see cref="System.Decimal"/> to be converted.</param>
		/// <returns>A NullableDecimal instance that wraps the <see cref="System.Decimal"/> value.</returns>
		public static implicit operator NullableDecimal(decimal decimalValue)
		{
			return new NullableDecimal(decimalValue);
		}

		/// <summary>
		/// Checks two NullableDecimal instances for equality.
		/// </summary>
		/// <param name="a">A NullableDecimal instance to test for equality.</param>
		/// <param name="b">A NullableDecimal instance to test for equality.</param>
		/// <returns><b>true</b> if both instances are the same; otherwise <b>false</b>.</returns>
		public static bool operator ==(NullableDecimal a, NullableDecimal b)
		{
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			else
			{
				return (a.DecimalValue == b.DecimalValue);
			}
		}

		/// <summary>
		/// Checks two NullableDecimal instances for inequality.
		/// </summary>
		/// <param name="a">A NullableDecimal instance to test for inequality.</param>
		/// <param name="b">A NullableDecimal instance to test for inequality.</param>
		/// <returns><b>true</b> if each instance is different; otherwise <b>false</b>.</returns>
		public static bool operator !=(NullableDecimal a, NullableDecimal b)
		{
			if (a == null && b == null)
			{
				return false;
			}
			else if (a == null || b == null)
			{
				return true;
			}
			else
			{
				return (a.DecimalValue != b.DecimalValue);
			}
		}

		/// <summary>
		/// Compares this instance to a specified object and returns an indication of their relative values.
		/// </summary>
		/// <param name="obj">An object to compare to this instance, or a null reference.</param>
		/// <returns>A signed integer that indicates the relative order of this instance and obj.</returns>
		public int CompareTo(object obj)
		{
			if (obj == null)
			{
				return 1;
			}
			else if (obj is NullableDecimal)
			{
				NullableDecimal nullableDecimal = obj as NullableDecimal;
				return decimalValue.CompareTo(nullableDecimal.DecimalValue);
			}
			else if (obj is decimal)
			{
				return decimalValue.CompareTo(obj);
			}
			else
			{
				throw new ArgumentException("The specified object was neither a decimal nor a NullableDecimal instance.");
			}
		}

		/// <summary>
		/// Returns a value indicating whether this instance is equal to a specified object.
		/// </summary>
		/// <param name="obj">An object to compare to this instance.</param>
		/// <returns><b>true</b> if obj is a NullableDecimal and has the same value as this instance; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return decimalValue.Equals(obj);
		}

		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>A hash code for the current NullableDecimal.</returns>
		public override int GetHashCode()
		{
			return decimalValue.GetHashCode();
		}

		/// <summary>
		/// Converts the value of this instance to its equivalent string representation.
		/// </summary>
		/// <returns>The string representation of the value of this instance.</returns>
		public override string ToString()
		{
			return decimalValue.ToString();
		}
	}
}
